define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Star, $window, $timeout, GoPage, Auth, $ionicSlideBoxDelegate) {
		//数据模型
		$scope.input = new Object();
		$scope.input.keywords = '';
		$scope.input.keywords1 = '';
		$scope.input.keywords2 = '';
		$scope.input.keywords3 = '';
		$scope.input.keywords4 = '';
		$scope.selectIndex = 0;
		$scope.type = 'company';
		//排序
		$scope.order = 1; //默认最新
		$scope.order1 = 1; //默认最新
		$scope.order2 = 1; //默认最新
		$scope.order3 = 1; //默认最新
		$scope.order4 = 1; //默认最新
		$scope.nPage = 1;
		$scope.nPage1 = 1;
		$scope.nPage2 = 1;
		$scope.nPage3 = 1;
		$scope.nPage4 = 1;
		$scope.count = null;
		var order;
		var keywords;
		//动态设置高度
		$scope.$on('$ionicView.afterEnter', function() {
			var lan = 0; //这是状态栏，只有ios的APP才有
			var tab = 0;
			var bars = document.getElementById("nav-bar-title");
			var bar = ((bars != undefined) ? 44 : 0);
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) {
				lan = 20;
			}
			//整体高度
			var all = $window.innerHeight;
			var seckill_top1 = document.getElementById("seckill-top1").clientHeight;
			var star_top1 = document.getElementById("star-top1").clientHeight;
			//			var star_top2 = document.getElementById("star-top2").clientHeight;
			$scope.star_myObj = { //滚动条可视范围
				"height": all - lan - bar - tab - star_top1 - seckill_top1 + "px"
			};
		}, false)
		$scope.isLoad = false;
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			if($scope.selectIndex == 0) {
				$scope.type = 'company';
				order = $scope.order ;
				keywords = $scope.input.keywords;
			} else if($scope.selectIndex == 1) {
				$scope.type = 'type_1';
				order = $scope.orde1 ;
				keywords = $scope.input.keywords1;
			} else if($scope.selectIndex == 2) {
				$scope.type = 'type_2';
				order = $scope.order2 ;
				keywords = $scope.input.keywords2;
			} else if($scope.selectIndex == 3) {
				$scope.type = 'type_3';
				order = $scope.order3 ;
				keywords = $scope.input.keywords3;
			} else {
				$scope.type = 'type_4';
				order = $scope.order4 ;
				keywords = $scope.input.keywords4;
			}
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};

		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectIndex = index;
			if($scope.selectIndex == 0) {
				$scope.type = 'company';
				order = $scope.order ;
				keywords = $scope.input.keywords;
			} else if($scope.selectIndex == 1) {
				$scope.type = 'type_1';
				order = $scope.order1 ;
				keywords = $scope.input.keywords1;
			} else if($scope.selectIndex == 2) {
				$scope.type = 'type_2';
				order = $scope.order2 ;
				keywords = $scope.input.keywords2;
			} else if($scope.selectIndex == 3) {
				$scope.type = 'type_3';
				order = $scope.order3 ;
				keywords = $scope.input.keywords3;
			} else {
				$scope.type = 'type_4';
				order = $scope.order4 ;
				keywords = $scope.input.keywords4;
			}
		};
		
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		//加载数据
		$scope.init_data = function() {
			//			$scope.user_id = Auth.get_user_id(); //判断是否登录了
			//判断是否为制造商或者品牌运营商
			if(Auth.check_user_type() == 2 || Auth.check_user_type() == 1)
				$scope.is_show = true;
			else
				$scope.is_show = false;
			Star.get_list('company', $scope.input.keywords, 1, 1, function(data) {
				$scope.listData = data;
			});
			Star.get_list('type_1', $scope.input.keywords1, 1, 1, function(data) {
				$scope.listData1 = data;
			});
			Star.get_list('type_2', $scope.input.keywords2, 1, 1, function(data) {
				$scope.listData2 = data;
			});
			Star.get_list('type_3', $scope.input.keywords3, 1, 1, function(data) {
				$scope.listData3 = data;
			});
			Star.get_list('type_4', $scope.input.keywords4, 1, 1, function(data) {
				$scope.listData4 = data;
			});
		}
		setTimeout(function(){
			$scope.init_data();
		},1000);
		//页面跳转
		$scope.onshow_stardetail = function(id) {
			GoPage.go_states("stardetail", {
				'detail_id': id
			});
		}
		$scope.onshow_starbusiness_pushinfo = function() {
			GoPage.go_states("starbusiness_pushinfo");
		}
		$scope.onshow_starbusiness_publicity = function() {
			GoPage.go_states("starbusiness_publicity");
		}

		$scope.orderBy = function(order) {
			if($scope.selectIndex == 0) {
				$scope.order = order ;
			} else if($scope.selectIndex == 1) {
				$scope.order1 = order ;
			} else if($scope.selectIndex == 2) {
				$scope.order2 = order ;
			} else if($scope.selectIndex == 3) {
				$scope.order3 = order ;
			} else {
				$scope.order4 = order ;
			}
			Star.get_list($scope.type, $scope.input.keywords, 1, order, function(data) {
				if($scope.selectIndex == 0) {
					$scope.listData = data;
					$scope.nPage = 1;
				} else if($scope.selectIndex == 1) {
					$scope.listData1 = data;
					$scope.nPage1 = 1;
				} else if($scope.selectIndex == 2) {
					$scope.listData2 = data;
					$scope.nPage2 = 1;
				} else if($scope.selectIndex == 3) {
					$scope.listData3 = data;
					$scope.nPage3 = 1;
				} else {
					$scope.listData4 = data;
					$scope.nPage4 = 1;
				}
			});
		}

		/*==========下拉刷新 判断哪个模块进行刷新=-=========*/
		//		$scope.isLoad = false; //可以加载
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.isLoad = true; //防止重复加载
			var order;
			if($scope.selectIndex == 0) {
				order = $scope.order =1;
				keywords = $scope.input.keywords = '';
			} else if($scope.selectIndex == 1) {
				order = $scope.order1=1;
				keywords = $scope.input.keywords1 = '';
			} else if($scope.selectIndex == 2) {
				order = $scope.order2=1;
				keywords = $scope.input.keywords2 = '';
			} else if($scope.selectIndex == 3) {
				order = $scope.order3=1;
				keywords = $scope.input.keywords3 = '';
			} else {
				order = $scope.order4=1;
				keywords = $scope.input.keywords4 = '';
			}
			Star.get_list($scope.type, keywords, 1, order, function(data) {
					if(data != null) {
						if($scope.selectIndex == 0) {
							$scope.listData = data;
							$scope.nPage = 1;
						} else if($scope.selectIndex == 1) {
							$scope.listData1 = data;
							$scope.nPage1 = 1;
						} else if($scope.selectIndex == 2) {
							$scope.listData2 = data;
							$scope.nPage2 = 1;
						} else if($scope.selectIndex == 3) {
							$scope.listData3 = data;
							$scope.nPage3 = 1;
						} else {
							$scope.listData4 = data;
							$scope.nPage4 = 1;
						}
					} else {
						if($scope.selectIndex == 0) {
							$scope.listData = new Object();
							$scope.nPage = 1;
						} else if($scope.selectIndex == 1) {
							$scope.listData1 = new Object();
							$scope.nPage1 = 1;
						} else if($scope.selectIndex == 2) {
							$scope.listData2 = new Object();
							$scope.nPage2 = 1;
						} else if($scope.selectIndex == 3) {
							$scope.listData3 = new Object();
							$scope.nPage3 = 1;
						} else {
							$scope.listData4 = new Object();
							$scope.nPage4 = 1;
						}
					}
				},
				function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};

		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true; //防止重复加载
			var order;
			if($scope.selectIndex == 0) {
				if($scope.listData.advertis == undefined) {
					$scope.nPage = 0;

				} else
				if($scope.listData.page.count <= $scope.listData.advertis.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.listData.page.count < $scope.nPage * 10) {
						$scope.nPage -= 1;
						$scope.count = $scope.nPage * 10;
					} else {
						$scope.count = null;
					}
				}
				Star.get_list($scope.type, $scope.input.keywords, $scope.nPage + 1, order, function(data) {
						if(data.advertis.length != 0) {
							if($scope.listData.advertis != undefined) {
								if($scope.count != null)
									$scope.listData.advertis.splice($scope.count, $scope.listData.page.count - $scope.count);
								$scope.listData.advertis = $scope.listData.advertis.concat(data.advertis);
								$scope.nPage += 1;
							} else {
								$scope.listData = data;
							}
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);

					});
			} else if($scope.selectIndex == 1) {
				if($scope.listData1.advertis == undefined) {
					$scope.nPage1 = 0;

				} else
				if($scope.listData1.page.count <= $scope.listData1.advertis.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.listData1.page.count < $scope.nPage1 * 10) {
						$scope.nPage1 -= 1;
						$scope.count = $scope.nPage1 * 10;
					} else {
						$scope.count = null;
					}
				}
				Star.get_list($scope.type, $scope.input.keywords1, $scope.nPage1 + 1, order, function(data) {
						if(data.advertis.length != 0) {
							if($scope.listData1.advertis != undefined) {
								if($scope.count != null)
									$scope.listData1.advertis.splice($scope.count, $scope.listData1.page.count - $scope.count);
								$scope.listData1.advertis = $scope.listData1.advertis.concat(data.advertis);
								$scope.nPage1 += 1;
							} else {
								$scope.listData1 = data;
							}
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);

					});
			} else if($scope.selectIndex == 2) {
				if($scope.listData2.advertis == undefined) {
					$scope.nPage2 = 0;

				} else
				if($scope.listData2.page.count <= $scope.listData2.advertis.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.listData2.page.count < $scope.nPage * 10) {
						$scope.nPage2 -= 1;
						$scope.count = $scope.nPage2 * 10;
					} else {
						$scope.count = null;
					}
				}
				Star.get_list($scope.type, $scope.input.keywords2, $scope.nPage2 + 1, order, function(data) {
						if(data.advertis.length != 0) {
							if($scope.listData2.advertis != undefined) {
								if($scope.count != null)
									$scope.listData2.advertis.splice($scope.count, $scope.listData2.page.count - $scope.count);
								$scope.listData2.advertis = $scope.listData2.advertis.concat(data.advertis);
								$scope.nPage2 += 1;
							} else {
								$scope.listData2 = data;
							}
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);

					});
			} else if($scope.selectIndex == 3) {
				if($scope.listData3.advertis == undefined) {
					$scope.nPage3 = 0;

				} else
				if($scope.listData3.page.count <= $scope.listData3.advertis.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.listData3.page.count < $scope.nPage3 * 10) {
						$scope.nPage3 -= 1;
						$scope.count = $scope.nPage3 * 10;
					} else {
						$scope.count = null;
					}
				}
				Star.get_list($scope.type, $scope.input.keywords3, $scope.nPage3 + 1, order, function(data) {
						if(data.advertis.length != 0) {
							if($scope.listData3.advertis != undefined) {
								if($scope.count != null)
									$scope.listData3.advertis.splice($scope.count, $scope.listData3.page.count - $scope.count);
								$scope.listData3.advertis = $scope.listData3.advertis.concat(data.advertis);
								$scope.nPage3 += 1;
							} else {
								$scope.listData3 = data;
							}
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);

					});
			} else {
				if($scope.listData4.advertis == undefined) {
					$scope.nPage = 0;

				} else
				if($scope.listData4.page.count <= $scope.listData4.advertis.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if($scope.listData4.page.count < $scope.nPage4 * 10) {
						$scope.nPage4 -= 1;
						$scope.count = $scope.nPage4 * 10;
					} else {
						$scope.count = null;
					}
				}
				Star.get_list($scope.type, $scope.input.keywords4, $scope.nPage4 + 1, order, function(data) {
						if(data.advertis.length != 0) {
							if($scope.listData4.advertis != undefined) {
								if($scope.count != null)
									$scope.listData4.advertis.splice($scope.count, $scope.listData4.page.count - $scope.count);
								$scope.listData4.advertis = $scope.listData4.advertis.concat(data.advertis);
								$scope.nPage4 += 1;
							} else {
								$scope.listData4 = data;
							}
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);

					});
			}

		};
		$scope.doSearch = function() {
//			var order;
			if($scope.selectIndex == 0) {
				keywords = $scope.input.keywords ;
			} else if($scope.selectIndex == 1) {
				keywords = $scope.input.keywords1 ;
			} else if($scope.selectIndex == 2) {
				keywords = $scope.input.keywords2 ;
			} else if($scope.selectIndex == 3) {
				keywords = $scope.input.keywords3 ;
			} else {
				keywords = $scope.input.keywords4 ;
			}
			Star.get_list($scope.type, keywords, 1, order, function(data) {
				if($scope.selectIndex == 0) {
					$scope.listData = data;
					$scope.nPage = 1;
				} else if($scope.selectIndex == 1) {
					$scope.listData1 = data;
					$scope.nPage1 = 1;
				} else if($scope.selectIndex == 2) {
					$scope.listData2 = data;
					$scope.nPage2 = 1;
				} else if($scope.selectIndex == 3) {
					$scope.listData3 = data;
					$scope.nPage3 = 1;
				} else {
					$scope.listData4 = data;
					$scope.nPage4 = 1;
				}
			});
		}

	}

	controller.$inject = ['$scope', 'Star', '$window', '$timeout', 'GoPage', 'Auth', '$ionicSlideBoxDelegate'];

	return app.registerController("starctrl", controller);
});